home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12244 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  127 lines

  1. Path: news1.io.org!news
  2. From: ticica@io.org (Ivan)
  3. Newsgroups: comp.lang.c++
  4. Subject: What is wrong with this code?
  5. Date: 19 Mar 1996 00:57:27 GMT
  6. Organization: Internex Online (io.org), Toronto, Ontario, Canada
  7. Message-ID: <4il0pn$fp@news1.io.org>
  8. NNTP-Posting-Host: dyna-125.net7b.io.org
  9. Mime-Version: 1.0
  10. Content-Type: Text/Plain; charset=US-ASCII
  11. X-Newsreader: WinVN 0.99.6
  12.  
  13.  
  14.   Please, can someone tell me what's wrong with this program? It's driving 
  15. me nuts.  When I compile it with Borland C++ 3.1 I get null pointer 
  16. assignment. With Watcom C++ 9.5 the last line (cout << a * 3) prints some 
  17. junk. Thanks in advance.
  18.     
  19.                             Ivan
  20. -------------------------------------------------------------------------
  21.  
  22.  
  23. #include <malloc.h>
  24. #include <stdlib.h>
  25. #include <iostream.h>
  26. #include <process.h>
  27. #include <stdarg.h>
  28. #include <string.h>
  29.  
  30. typedef double datatype; // matrices and vectors will contain real numbers
  31.  
  32. void memoryerror (void) {
  33.       cout << "Not enough memory to allocate buffer\n";
  34.       exit(1);  /* terminate program if out of memory */
  35. }
  36.  
  37. class vector {
  38.       private:
  39.               int dim;          // dimension of vector
  40.               datatype *data;   // the contents of vector (dynamicly      
  41.                            // allocated)
  42.               char *name;       // Just an ID for a vector
  43.       public:
  44.                 vector (char* , int , ...); // parameters are the     
  45.                     // dimension and values
  46.                 vector (void);      // sets dim to -1 (for later     
  47.                 // interactive input).
  48.                 ~vector (void);     // destructor
  49.  
  50.                 friend ostream &operator << (ostream &, const vector &);
  51.  
  52.                 friend vector operator *(vector &, datatype);
  53.  
  54. };
  55.  
  56. vector::vector (char *newname, int dimension, ...)
  57. {
  58.    dim = dimension;
  59.    if (!(data = new datatype[dim]))
  60.       memoryerror();
  61.  
  62.    if (!(name = new char[strlen(newname)+1]))
  63.       memoryerror();
  64.    strcpy (name, newname);
  65.  
  66.    va_list ap;
  67.    float arg;
  68.    va_start(ap, dimension);
  69.    for (int i=0;i<dimension;i++) {
  70.        data[i] = va_arg(ap, datatype);
  71.    }
  72.    va_end(ap);
  73. }
  74.  
  75. vector::~vector (void) {
  76.   if (data) delete data;
  77.   if (name) delete name;
  78. }
  79.  
  80. vector::vector (void)
  81. {
  82.     dim = -1;
  83.     data = NULL;
  84.     name = new char[8];
  85.     strcpy (name, "unnamed");
  86. }
  87.  
  88. ostream &operator << (ostream &output, const vector &vec)
  89. {
  90.     output << "\nVector '" << vec.name << "' is of dimension " << vec.dim;
  91.     output << " and it's parameters are:\n";
  92.     for (int i = 0; i < vec.dim; i++) {
  93.         output << '\n' << vec.name << "[" << i+1 << "] = " << vec.data[i]; 
  94.     }
  95.  
  96.     return output;
  97. }
  98.  
  99. vector operator * (vector &vec, datatype a)
  100. {
  101.     vector vecret;
  102.     vecret.dim = vec.dim;
  103.     if (!(vecret.data = new datatype[vec.dim]))
  104.        memoryerror();
  105.  
  106.     for (int i=0;i<vec.dim;i++) {        // copy all the values
  107.        vecret.data[i] = vec.data[i] * a;
  108.     }
  109.     return vecret;
  110. }
  111.  
  112. void main(void)
  113. {
  114.     vector a("first", 4, 1.0, 2.0, 3.0, 4.0);
  115.     vector b("second", 2, 1.1, 1.2);
  116.     cout << a;
  117.     cout << b;
  118.     cout << a * 3;
  119. }
  120.  
  121.  
  122.  
  123.  
  124.  
  125.     
  126.  
  127.